//
// File:	Variant.cpp
// Author:	Thomas TISSOT-DUPONT
// Login:	<thomas.tissotdupont@free.fr>
//
// Created:	28/04/2010 at 00:35:26
// Purpose:	
//

#include <map>
#include "Variant.hpp"

//------------------------------------------------------------------
// Constructor / Destructor
//------------------------------------------------------------------
Variant::Variant()
	:	m_value(),
		m_bValue(false),
		m_iValue(0),
		m_uValue(0),
		m_flValue(0.0f)
{
}

Variant::Variant(bool b)
	:	m_value(),
		m_bValue(b),
		m_iValue(static_cast<int>(b)),
		m_uValue(static_cast<unsigned int>(b)),
		m_flValue(static_cast<float>(b))
{
	m_value = ConvertToString(b);
}

Variant::Variant(int i)
	:	m_value(),
		m_bValue(!i ? false : true),
		m_iValue(i),
		m_uValue(static_cast<unsigned int>(i)),
		m_flValue(static_cast<float>(i))
{
	m_value = ConvertToString(i);
}

Variant::Variant(unsigned int u)
	:	m_value(),
		m_bValue(!u ? false : true),
		m_iValue(static_cast<int>(u)),
		m_uValue(u),
		m_flValue(static_cast<float>(u))
{
	m_value = ConvertToString(u);
}

Variant::Variant(float f)
	:	m_value(),
		m_bValue(f == 0.0f ? false : true),
		m_iValue(static_cast<int>(f)),
		m_uValue(static_cast<unsigned int>(f)),
		m_flValue(f)
{
	m_value = ConvertToString(f);
}

Variant::Variant(const std::string &source)
	:	m_value(source),
		m_bValue(false),
		m_iValue(0),
		m_uValue(0),
		m_flValue(0.0f)
{
	//Init
	std::map<std::string, bool> boolValues;
	boolValues["true"] = true;
	boolValues["false"] = false;

	//Perform conversion
	m_iValue = ConvertFromString<int>(source);
	m_uValue = ConvertFromString<unsigned int>(source);
	m_flValue = ConvertFromString<float>(source);
	m_bValue = (boolValues.find(source) != boolValues.end()) ? boolValues[source] : ConvertFromString<bool>(source);
}
